/**
 * Project Name:mixc
 * File Name:Response
 * Package Name:com.crland.mixc.framework.common
 * Date:2017/3/29 16:43
 * Copyright (c) 2017, crland.com.cn All Rights Reserved.
 */

package com.xf.lastmall.order.api.common;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

import java.io.Serial;
import java.io.Serializable;

/**
 * ClassName: Response <br/>
 * Function: <br/>
 * Reason: <br/>
 * Date: 2017/3/29 16:43 <br/>
 *
 * @author xushipeng
 * @version <br/>
 * @see
 */
@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    private String code = Status.SUCCESS.getCode();// 状态码
    private String message = Status.SUCCESS.getName();// 状态信息
    private T data;

    public Response() {
    }

    public Response(T data) {
        this.data = data;
    }

    public Response(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public Response(Status status, String message) {
        this.code = status.getCode();
        this.message = message;
    }

    public Response(T data, String code, String message) {
        this.data = data;
        this.code = code;
        this.message = message;
    }

    public static <T> Response<T> success() {
        return new Response<>(null);
    }

    public static <T> Response<T> success(T data) {
        return new Response<>(data);
    }

    public static <T> Response<T> failure() {
        return new Response<>(null, Status.FAILED.getCode(), Status.FAILED.getName());
    }

    public static <T> Response<T> failure(T data) {
        return new Response<>(data, Status.FAILED.getCode(), Status.FAILED.getName());
    }

    public static <T> Response<T> failure(String code, String message) {
        return failure(null, code, message);
    }

    public static <T> Response<T> failure(T data, String code, String message) {
        return new Response<>(data, code, message);
    }

    public static <T> Response<T> failureWithoutData(String errorMsg) {
        return new Response<>(null, Status.FAILED.getCode(), errorMsg);
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return code.equals(Status.SUCCESS.getCode());
    }


    public enum Status {
        SUCCESS("0", "成功"),
        FAILED("-1", "失败"),
        UNKNOW("-2", "状态未知"),
        DOING("600", "处理中");

        private String code;
        private String name;

        Status(String code, String name) {
            this.code = code;
            this.name = name;
        }

        public String getCode() {
            return this.code;
        }

        public String getName() {
            return this.name;
        }
    }
}
